package sg.edu.nus.ouroboros.activities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import sg.edu.nus.ouroboros.R;
import sg.edu.nus.ouroboros.helper.MyHttpClient;
import sg.edu.nus.ouroboros.helper.MyParser;
import sg.edu.nus.ouroboros.vo.TimeTableVO;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainScreenActivity extends ListActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		try{
			super.onCreate(savedInstanceState);
			setContentView(R.layout.mainscreen); 

			//TextView label = (TextView) findViewById(R.id.welcome_text);
			Intent intent = getIntent();
			/*Bundle extras = intent.getExtras();
			String token = extras.getString("IVLE_token");
			 */
			//String data = MyHttpClient.makeRequest(URLHelper.insertAuthToken(AppConstants.MODULE_URL, token));
			//String data = MyHttpClient.makeRequest(URLHelper.insertAuthToken(AppConstants.TIME_TABLE_URL, token));
			final Button button = (Button) findViewById(R.id.button);
			button.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// Perform action on clicks
					Toast.makeText(MainScreenActivity.this, "Beep Bop", Toast.LENGTH_SHORT).show();
				}
			});


			String data = MyHttpClient.makeRequest("soumo");
			System.out.println(data);
			List<TimeTableVO> listData = MyParser.parseTimeTableString(data);
			List<HashMap<String, String>> list = populateList(listData); 
			ListAdapter adapter = new SimpleAdapter(this, list, R.layout.module_row_layout, new String[] {"module_code","day_text","venue"}, new int[] {R.id.module_code,R.id.day_text,R.id.venue}){
				public View getView(int pos, View vw, ViewGroup parent){
					if(vw != null && vw.getClass().equals(RelativeLayout.class)){
						System.out.println("mila");
					}
					return super.getView(pos, vw, parent);
				}
			};
			setListAdapter(adapter);
			System.out.println(adapter.getCount());
		}catch(Exception e){
			System.out.println(e.getMessage());
		}

	}

	private List<HashMap<String, String>> populateList(List<TimeTableVO> data) {
		List<HashMap<String, String>> dataList  = new ArrayList<HashMap<String,String>>();
		HashMap<String,String> map = null;
		for(TimeTableVO item:data){
			map = new HashMap<String,String>();
			map.put("module_code",item.getModuleCode());
			map.put("day_text",item.getDayText());
			map.put("venue", item.getVenue());
			dataList.add(map);
		}
		return dataList;

	}
}
